package com.oj.controller;


import com.oj.entity.R;
import com.oj.entity.User;
import com.oj.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags="用户管理")
public class UserController {


    @Autowired
     private  UserService userService;

    @PostMapping("/login")
    @ApiModelProperty(value = "登录")
    public R<User>login(HttpServletRequest request, @RequestBody User user){

        //1、将页面提交的用户名密码使用md5加密
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.到数据库里查询用户名密码
        User us = userService.selectUser(user.getUserName());

        //3、没查询到
        if(us==null){
            log.error("查询到的user对象为空");
            return R.error("登录失败");
        }

        //4、密码不对
        if(!us.getPassword().equals(password)){
            log.error("密码不对");
            return  R.error("登录失败");
        }


        //5、登录成功，用jwt处理下


        return null;
    }

}
